"""
Author        yongfa
Date          2023-08-31 03:37:44
LastEditTime  2023-08-31 05:58:04
LastEditors   yongfa
Description   路由
"""

from flask_restx import Resource

from app.extensions.decorators import header_required
from .models import Company, CompanyAccessKey
from .parameters import set_params
from .services import CompanyService, CompanyAccessKeyService


def configure_router(ns):
    @ns.route("")
    class CompanyListResource(Resource):
        method_decorators = [header_required]

        @ns.doc(parser=set_params['get_list_params'], description="获取公司信息列表")
        @ns.expect(set_params['get_list_params'])
        def get(self):
            """获取公司信息列表"""
            return CompanyService(model=Company,
                                  args=set_params['get_list_params'], filter_delete=True).get_list()

        @ns.doc(parser=set_params['create_params'], description="添加公司信息")
        @ns.expect(set_params['create_params'])
        def post(self):
            """添加公司信息"""
            return CompanyService(model=Company, args=set_params['create_params'], filter_delete=True).create()

    @ns.route("/<int:id>")
    @ns.doc(params={"id": "公司信息id"})
    class CompanyDetailResource(Resource):
        method_decorators = [header_required]

        def get(self, id):
            """获取公司信息详情"""
            return CompanyService(model=Company, args=None, filter_delete=True).get_detail(id)

        @ns.doc(parser=set_params['update_params'], description="更新公司信息")
        @ns.expect(set_params['update_params'])
        def put(self, id):
            """更新公司信息"""
            return CompanyService(model=Company, args=set_params['update_params'], filter_delete=True).update(id)

        @ns.doc(description="删除公司信息")
        def delete(self, id):
            """删除公司信息"""
            return CompanyService(model=Company, args=None, filter_delete=True).soft_delete(id)

    @ns.route("/accessKey")
    class CompanyAccessKeyListResource(Resource):
        method_decorators = [header_required]

        @ns.doc(parser=set_params['get_list_access_params'], description="获取公司密钥信息列表")
        @ns.expect(set_params['get_list_access_params'])
        def get(self):
            """获取公司密钥信息列表"""
            return CompanyAccessKeyService(model=CompanyAccessKey,
                                           args=set_params['get_list_access_params']).get_list()

        @ns.doc(parser=set_params['create_access_params'], description="添加公司密钥信息")
        @ns.expect(set_params['create_access_params'])
        def post(self):
            """添加公司密钥信息"""
            return CompanyAccessKeyService(model=CompanyAccessKey,
                                           args=set_params['create_access_params']).create()

    @ns.route("/accessKey/<int:id>")
    @ns.doc(params={"id": "公司密钥信息id"})
    class CompanyAccessKeyDetailResource(Resource):
        method_decorators = [header_required]

        def get(self, id):
            """获取公司密钥信息详情"""
            return CompanyAccessKeyService(model=CompanyAccessKey, args=None).get_detail(id)

        @ns.doc(parser=set_params['update_access_params'], description="更新公司密钥信息")
        @ns.expect(set_params['update_access_params'])
        def put(self, id):
            """更新公司密钥信息"""
            return CompanyAccessKeyService(model=CompanyAccessKey,
                                           args=set_params['update_access_params']).update(id)

        @ns.doc(description="删除公司密钥信息")
        def delete(self, id):
            """删除公司密钥信息"""
            return CompanyAccessKeyService(model=CompanyAccessKey, args=None).delete(id)
